//其他装置
var Device = {
    headerHeight: 40,//标题栏高度
    width:360,//默认宽度
    headerFontFamily:'Courier New',
    headerFontSize:16,
    headerFontColor: '#ffffff',
    headerBackground: '#04a69b',//标题栏底色

    bodyBackground:'#cdcdcd',//内部容器底色
    portBackground: '#3f7d78',//端口模块底色
    portVerSpace:4,//端口模块竖向间隔
    portVerSpaceBackground:'#8eafb8',//端口模块竖向间隔底色
    createNew: function(id,name){
        var device = {};
        device.x=0;
        device.y=0;
        device.width=Device.width;
        device.id = id;
        device.name = name;
        device.ports = [];
        device.deviceIconWidth = 40;
        device.deviceIconHeight = 50;
        device.draw = function(canvas){
            Device.draw(this, canvas);
        };
        device.tryInsertPortByInfo = function(portinfo){
            /*{
                PORT_ID:"",
                NAME:"",
                PLUG:"",
                DIRECTION:""
            }*/
            var thePort = null;
            for(var k in this.ports){
                var p = this.ports[k];
                if(p.NAME == portinfo.NAME){
                    thePort = p;
                    break;
                }
            }
            if(portinfo.DIRECTION == 'RX'){//接收
                if(thePort == null){
                    thePort = Port.createNew(portinfo.PORT_ID,null,portinfo.NAME);
                    thePort.NAME = portinfo.NAME;
                    this.ports.push(thePort);
                }
                thePort.if_id = portinfo.PORT_ID;
            } else {//发送
                if(thePort == null){
                    thePort = Port.createNew(null,portinfo.PORT_ID,portinfo.NAME);
                    thePort.NAME = portinfo.NAME;
                    this.ports.push(thePort);
                }
                thePort.of_id = portinfo.PORT_ID;
            }
        };
        return device;
    },
    draw: function(obj,canvas){
        if(obj.ports.length < 1){
            obj.height = Port.height;
        }else{
            obj.height = obj.ports.length * Port.height + (obj.ports.length - 1) * Device.portVerSpace;
        }
        //绘制主体色
        canvas.fillStyle = Device.headerBackground;
        canvas.fillRect(obj.x,obj.y + 2,obj.width,obj.height - 4);
        //小图标
        var img = new Image();    
        img.src = config.icon_device_url;
        if(obj.info!=undefined && obj.info.UNIT_CLASS == 'SWITCH'){
            img.src = config.icon_switch_url;
        }else if(obj.info!=undefined && obj.info.UNIT_CLASS == 'ODF'){
            img.src = config.icon_odf_url;
        }
        if(img.complete){
            canvas.drawImage(img,obj.x + 5, obj.y + obj.height / 2 - obj.deviceIconHeight / 2, obj.deviceIconWidth , obj.deviceIconHeight);
        }else{
            img.onload = function(){
                canvas.drawImage(img,obj.x + 5, obj.y + obj.height / 2 - obj.deviceIconHeight / 2, obj.deviceIconWidth , obj.deviceIconHeight);
            }
        }
        //绘制标题
        canvas.font = Device.headerFontSize + "px " + Device.headerFontFamily;
        canvas.fillStyle = Device.headerFontColor;
        canvas.textAlign="left";
        canvasTextAutoLine(obj.name,canvas,obj.x + 10 + obj.deviceIconWidth,obj.y, obj.width - obj.deviceIconWidth - 20, obj.height,Device.headerFontSize + 10,Device.headerFontSize,'middle');
        var port = obj.port;
        port.bodyBackground = Device.portBackground;
        if(port.direct == 'left'){
            port.x = obj.x - Port.width;
        }else{
            port.x = obj.x + obj.width;
        }
        port.y = obj.y;
        port.draw(canvas);
        //绘制上下边框
        var left = obj.x;
        var right = obj.x + obj.width;
        if(port.direct == 'left'){
            left -= Port.width;
        }else{
            right += Port.width;
        }
        canvas.beginPath();
        canvas.strokeStyle = Device.bodyBackground;
        canvas.lineWidth=2;
        canvas.moveTo(left,obj.y);
        canvas.lineTo(right,obj.y);
        canvas.lineTo(right,obj.y + obj.height - 2);
        canvas.lineTo(left,obj.y + obj.height - 2);
        canvas.lineTo(left,obj.y);
        canvas.stroke();
    },
    drawTitle:function(obj, canvas){
        //绘制标题栏
        canvas.fillStyle = Device.headerBackground;
        canvas.fillRect(obj.x, obj.y, obj.width, Device.headerHeight);
        //小图标
        var img = new Image();    
        img.src = config.icon_device_url;
        if(obj.data.UNIT_CLASS == 'SWITCH'){
            img.src = config.icon_switch_url;
        }else if(obj.data.UNIT_CLASS == 'ODF'){
            img.src = config.icon_odf_url;
        }
        if(img.complete){
            canvas.drawImage(img,obj.x + 5, obj.y + 5,Device.headerHeight - 10 ,Device.headerHeight - 10);
        }else{
            img.onload = function(){
                canvas.drawImage(img,obj.x + 5, obj.y + 5,Device.headerHeight - 10 ,Device.headerHeight - 10);
            }
        }
        //绘制标题
        canvas.font = Device.headerFontSize + "px " + Device.headerFontFamily;
        canvas.fillStyle = Device.headerFontColor;
        canvas.textAlign="left";
        canvas.fillText(obj.name,obj.x + Device.headerHeight, obj.y + (Device.headerHeight + Device.headerFontSize) / 2 -2,obj.width - Device.headerHeight - 10);
    },
    drawBody:function(obj, canvas){
        //绘制主体色
        canvas.fillStyle = Device.headerBackground;
        canvas.fillRect(obj.x,obj.y + 2,obj.width,obj.height - 4);
        //小图标
        var img = new Image();    
        img.src = config.icon_device_url;
        if(obj.UNIT_CLASS == 'SWITCH'){
            img.src = config.icon_switch_url;
        }else if(obj.UNIT_CLASS == 'ODF'){
            img.src = config.icon_odf_url;
        }else if(obj.UNIT_CLASS == 'CABINET'){
            img.src = config.icon_cabinet_url;
        }
        if(img.complete){
            canvas.drawImage(img,obj.x + 5, obj.y + obj.height / 2 - obj.deviceIconHeight / 2, obj.deviceIconWidth , obj.deviceIconHeight);
        }else{
            img.onload = function(){
                canvas.drawImage(img,obj.x + 5, obj.y + obj.height / 2 - obj.deviceIconHeight / 2, obj.deviceIconWidth , obj.deviceIconHeight);
            }
        }
        //绘制标题
        canvas.font = Device.headerFontSize + "px " + Device.headerFontFamily;
        canvas.fillStyle = Device.headerFontColor;
        canvas.textAlign="left";
        canvasTextAutoLine(obj.name,canvas,obj.x + 10 + obj.deviceIconWidth,obj.y, obj.width - obj.deviceIconWidth - 20, obj.height,Device.headerFontSize + 10,Device.headerFontSize,'middle');
        //绘制上下边框
        var left = obj.x;
        var right = obj.x + obj.width;
        canvas.beginPath();
        canvas.strokeStyle = Device.bodyBackground;
        canvas.lineWidth=2;
        canvas.moveTo(left,obj.y);
        canvas.lineTo(right,obj.y);
        canvas.lineTo(right,obj.y + obj.height);
        canvas.lineTo(left,obj.y + obj.height);
        canvas.lineTo(left,obj.y);
        canvas.stroke();
    }
};